package cn.puge.mertisfront.service.impl;

import cn.puge.mertisfront.entity.LikePubVo.likePubishVo;
import cn.puge.mertisfront.entity.LikePubVo.likeVo;
import cn.puge.mertisfront.entity.PugeLike;
import cn.puge.mertisfront.entity.PugePubish;
import cn.puge.mertisfront.entity.PugeUser;
import cn.puge.mertisfront.mapper.PugePubishMapper;
import cn.puge.mertisfront.service.PugeLikeService;
import cn.puge.mertisfront.service.PugePubishService;
import cn.puge.mertisfront.service.PugeUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LIJW
 * @since 2020-09-18
 */
@Service
public class PugePubishServiceImpl extends ServiceImpl<PugePubishMapper, PugePubish> implements PugePubishService {

    @Autowired
    private PugeUserService pugeUserService;

    @Autowired
    private PugeLikeService pugeLikeService;

    //查询所有处罚信息 和点赞用户信息
    @Override
    public List<likePubishVo> getAllPubish() {


        //根据发布时间查询所有处罚
        QueryWrapper<PugePubish> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("gmt_create");
        List<PugePubish> pugePubishes = baseMapper.selectList(queryWrapper);
        List<likePubishVo> likePubishVos = new ArrayList<>();
        for (PugePubish pugePubish : pugePubishes) {
            likePubishVo likePubishVo = new likePubishVo();
            BeanUtils.copyProperties(pugePubish,likePubishVo);
            //获取发布人的信息
            QueryWrapper<PugeUser> queryWrapper2 = new QueryWrapper<>();
             queryWrapper2.eq("name", pugePubish.getFbpeople());
            PugeUser PbPugeUser = pugeUserService.getOne(queryWrapper2);
            BeanUtils.copyProperties(PbPugeUser,likePubishVo);
            likePubishVo.setId(pugePubish.getId());
            likePubishVo.setGmtCreate(pugePubish.getGmtCreate());
            //查询当前处罚点赞的人
            QueryWrapper<PugeLike> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("pubish_id",pugePubish.getId());
            List<PugeLike> pugeLikes = pugeLikeService.list(queryWrapper1);
            //拿到点赞人的id
            List<String> userIds = new ArrayList<>();
            for (PugeLike pugeLike : pugeLikes) {
                userIds.add(pugeLike.getUserId());
            }

            //批量查询点赞人 并获取所有人的名字
            List<PugeUser> pugeUsers = null;
            List<likeVo> userName = new ArrayList<>();
            //无人点赞  没有人点赞抛出异常,继续执行下面判断为空的代码
            try {
                pugeUsers = (List<PugeUser>) pugeUserService.listByIds(userIds);
            } catch (Exception e) {
            }

            //判断是否有人点赞
            if (StringUtils.isEmpty(pugeUsers)){

            }else {
                //有人点赞
                for (PugeUser pugeUser : pugeUsers) {
                    likeVo likeVo = new likeVo();
                    BeanUtils.copyProperties(pugeUser,likeVo);
                    userName.add(likeVo);
                }
            }
            //点赞人数放到最后封装类
            likePubishVo.setLikeName(userName);
            likePubishVos.add(likePubishVo);
        }


        return likePubishVos;
    }
}
